package com.jinke.api.modules.third.huaLei.tool;

import com.alibaba.fastjson2.JSONObject;
import com.google.common.collect.Maps;
import com.jinke.api.common.constant.RetryTypeConstants;
import com.jinke.api.common.util.OKHttpRetryRequestUtil;
import com.jinke.api.modules.third.ezeeship.model.request.EstimateRateRequest;
import com.jinke.api.modules.third.huaLei.enums.DutyType;
import com.jinke.api.modules.third.huaLei.enums.ReturnSign;
import com.jinke.api.modules.third.huaLei.enums.TradeType;
import com.jinke.api.modules.third.huaLei.model.constants.HuaLeiApiConstants;
import com.jinke.api.modules.third.huaLei.model.dto.HuaLeiCreateOrderDTO;
import com.jinke.api.modules.third.huaLei.model.dto.HuaLeiSelectAuthDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @projectName:
 * @title: HuaLeiRequest
 * @description: 华磊请求类
 * @see:
 * @author: hhp
 * @version: 1.0.0
 * @createTime: 2024/8/29 9:11
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class HuaLeiRequest {
    @Value("${hua-lei.create-order-api-prefix}")
    private String createOrderApiPrefix;
    @Value("${hua-lei.print-label-api-prefix}")
    private String printLabelApiPrefix;
    @Value("${hua-lei.username}")
    private String username;
    @Value("${hua-lei.password}")
    private String password;
    private final Environment environment;
    private final OKHttpRetryRequestUtil okHttpRetryRequest;

    /**
     * @Author: hhp
     * @Description: 华磊身份认证
     * @Data: 2024/8/29 9:41
     * @param
     * @return: java.lang.String
     * @throws:
     **/
    public String selectAuth() {
        log.info("==========华磊身份认证请求开始================请求参数:[username:{}, password:{}]", username, password);
        Map<String, String> headers = Maps.newHashMap();
        //headers.put("Authorization", getApiToken());
        String responseJson = Optional.ofNullable(
                okHttpRetryRequest.sendGetRequest(
                        RetryTypeConstants.HUA_LEI_SELECT_AUTH,
                        new StringBuilder(createOrderApiPrefix)
                                .append(HuaLeiApiConstants.SELECT_AUTH)
                                .append("?username=").append(username)
                                .append("&password=").append(password).toString(),
                        headers)
        ).orElse("{}");
        log.info("==========华磊身份认证请求结束================响应结果:[{}]", responseJson);
        return responseJson;
    }

    /**
     * @Author: hhp
     * @Description: 华磊渠道列表
     * @Data: 2024/8/29 10:10
     * @param
     * @return: java.lang.String
     * @throws:
     **/
    public String getProductList() {
        log.info("==========华磊渠道列表请求开始================请求参数:[]");
        Map<String, String> addHeaders = Maps.newHashMap();
        addHeaders.put("Content-Type", "application/json");
        //addHeaders.put("ta-version", "v2");
        Map<String, String> headers = Maps.newHashMap();
        //headers.put("Authorization", getApiToken());
        String responseJson = Optional.ofNullable(
                okHttpRetryRequest.sendPostRequest(
                        RetryTypeConstants.HUA_LEI_PRODUCT_LIST,
                        "application/json",
                        createOrderApiPrefix + HuaLeiApiConstants.GET_PRODUCT_LIST,
                        "",
                        false, Maps.newHashMap(),
                        addHeaders, headers)
        ).orElse("[]");
        log.info("==========华磊渠道列表请求结束================响应结果:[{}]", responseJson);
        return responseJson;
    }

    /**
     * @Author: hhp
     * @Description: 华磊添加订单
     * @Data: 2024/8/29 14:32
     * @param estimateRateRequest
     * @return: java.lang.String
     * @throws:
     **/
    public String createOrder(EstimateRateRequest estimateRateRequest) {
        log.info("==========华磊添加订单请求开始================请求参数:[]");
        Map<String, String> addHeaders = Maps.newHashMap();
        addHeaders.put("Content-Type", "application/json");
        //addHeaders.put("ta-version", "v2");
        Map<String, String> headers = Maps.newHashMap();
        //headers.put("Authorization", getApiToken());
        List<HuaLeiCreateOrderDTO.OrderInvoiceParam> orderInvoiceParam = Lists.newArrayList();
        orderInvoiceParam.add(new HuaLeiCreateOrderDTO.OrderInvoiceParam().setInvoiceAmount("1").setInvoicePcs("2").setInvoiceTitle("test"));
        // todo estimateRateRequest 转 HuaLeiCreateOrderDTO
        String strParam="{\n" +
                "    \"buyerid\": \"\",\n" +
                "    \"order_customerinvoicecode\": \"TEST02\",\n" +
                "    \"product_id\": \"882\",\n" +
                "    \"trade_type\": \"ZYXT\",\n" +
                "    \"country\": \"US\",\n" +
                "    \"consignee_city\": \"Fountain Valley\",\n" +
                "    \"consignee_telephone\": \"14246340093\",\n" +
                "    \"consignee_name\": \"Martin Ventress\",\n" +
                "    \"consignee_postcode\": \"92708\",\n" +
                "    \"consignee_state\": \"California\",\n" +
                "    \"consignee_address\": \"10059 Quail Ct Fountain Valley California United States\",\n" +
                "    \"customer_id\": \"43522\",\n" +
                "    \"customer_userid\": \"41641\",\n" +
                "    \"orderInvoiceParam\": [\n" +
                "        {\n" +
                "            \"invoice_amount\": \"11.00\",\n" +
                "            \"sku\": \"Strap Buckle 表带  Isssss\",\n" +
                "            \"invoice_title\": \"Strap Buckle Issssss\",\n" +
                "            \"invoice_pcs\": \"1\",\n" +
                "            \"invoice_weight\": \"220\"\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        Map<String, String> queryParamMap = Maps.newHashMap();
        queryParamMap.put("param", strParam);
        String responseJson = Optional.ofNullable(
                okHttpRetryRequest.sendPostRequest(
                        RetryTypeConstants.HUA_LEI_CREATE_ORDER,
                        "application/x-www-form-urlencoded; charset=utf-8",
                        createOrderApiPrefix + HuaLeiApiConstants.CREATE_ORDER_API,
                        strParam,
                        true,
                        queryParamMap,
                        addHeaders, headers)
        ).orElse("{}");
        log.info("==========华磊添加订单请求结束================响应结果:[{}]", responseJson);
        return responseJson;
    }
}
